// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {globalConfig} from "../config/globalConfig";
import {model} from "../web/model/Model";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MenuHead extends cc.Component {

  @property([cc.Node])
  stars: [cc.Node] = []

  @property(cc.Sprite)
  icon: cc.Sprite = null;

  @property(cc.Sprite)
  achievementIcon: cc.Sprite = null;

  // LIFE-CYCLE CALLBACKS:

  onLoad() {
  }

  start() {
    this.setGrade(model.user.msg.level)
  }

  setGrade(num: number) {
    const config: any = globalConfig.Grade[num - 1];
    this.setStar(config.star)
    this.setIconId(config.iconid)
  }

  private setStar(value: number) {
    this.stars.forEach(value1 => value1.active = false)
    this.stars.forEach((value1, index) => {
      if (index < value) {
        value1.active = true
      }
    })
  }

  setIconId(iconId: number) {
    this.achievementIcon.node.active = false;
    cc.loader.loadRes(`icon/icon_duanWeiS${iconId}`, (err, res) => {
      this.achievementIcon.spriteFrame = new cc.SpriteFrame(res);
      this.achievementIcon.node.active = true;
    });
  }

}
